/*
 * Angry Monsters, Windows Mobile 7 XNA Game FW demo
 * Tero Paananen, tepaanan@gmail.com
 * 2011 Finland
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace WP7Box2d
{
    class Menu : ButtonListenerIF
    {
        public Rectangle m_position;
        public Texture2D m_backgroundTexture = null;
        public Texture2D m_monsterTexture = null;
        
        private List<Button> m_buttons = new List<Button>();

        private ButtonListenerIF m_listener;

        public Menu(ButtonListenerIF listener)
        {
            m_listener = listener;
        }

        public void SetPosition(Rectangle p)
        {
            m_position = p;
        }

        public void LoadContent(ContentManager cm)
        {
            if (m_backgroundTexture == null)
            {
                Texture2D texture = cm.Load<Texture2D>("create_btn");

                m_monsterTexture = cm.Load<Texture2D>("bluemonster");

                int btnHeight = texture.Height;
                int btnCap = (m_position.Height - (3 * btnHeight)) / 5;
                int y = btnCap * 2 + m_position.Y;

                Button btn = new Button(this, Button.ButtonType.ECreateBtn, texture);
                btn.SetPosition(new Rectangle((m_position.Width - texture.Width) / 2, y, texture.Width, texture.Height));
                m_buttons.Add(btn);
                y += texture.Height;
                y += btnCap;

                texture = cm.Load<Texture2D>("play_btn");
                btn = new Button(this, Button.ButtonType.EPlayBtn, texture);
                btn.SetPosition(new Rectangle((m_position.Width - texture.Width) / 2, y, texture.Width, texture.Height));
                m_buttons.Add(btn);
                y += texture.Height;
                y += btnCap;

                texture = cm.Load<Texture2D>("exit_btn");
                btn = new Button(this, Button.ButtonType.EExitBtn, texture);
                btn.SetPosition(new Rectangle((m_position.Width - texture.Width) / 2, y, texture.Width, texture.Height));
                m_buttons.Add(btn);

                m_backgroundTexture = cm.Load<Texture2D>("menuback2");
            }
        }

        public void Update(GameTime gameTime)
        {
            TouchCollection touchCollection = TouchPanel.GetState();
            if (touchCollection.Count() > 0)
            {
                TouchLocation tl = touchCollection.First();
                Vector2 touchPosition = tl.Position;

                if (tl.State == TouchLocationState.Pressed)
                {
                    for (int i = 0; i < m_buttons.Count; i++)
                    {
                        m_buttons[i].handleTouch(touchPosition);
                    }
                }
            }
        }

        public void ButtonPressed(Button.ButtonType buttonType)
        {
            m_listener.ButtonPressed(buttonType);
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (m_backgroundTexture == null)
                return;
            
            spriteBatch.Draw(m_backgroundTexture, m_position, Color.White);
            
            for (int i = 0; i < m_buttons.Count; i++)
            {
                m_buttons[i].Draw(spriteBatch, gameTime);
            }

            spriteBatch.Draw(m_monsterTexture, new Rectangle(m_position.Width - m_monsterTexture.Width - 40,
                m_position.Height - m_monsterTexture.Height - 20,
                m_monsterTexture.Width,m_monsterTexture.Height), Color.White);
        }


    }
 }
